#include "geturlimage.h"
#include <QJsonParseError>
#include <QJsonArray>
#include <QJsonObject>
getUrlImage::getUrlImage(QObject *parent) : QObject(parent)
{
    m_TagStrImageUrlList.clear();
    manager = new QNetworkAccessManager(this);

    connect(manager, SIGNAL(finished(QNetworkReply*)),
              this, SLOT(replyFinished(QNetworkReply*)));


}



void getUrlImage::getImageUrlList(QString string)
{
    //    qWarning() << val;
        QJsonDocument d = QJsonDocument::fromJson(string.toLatin1());
        QJsonObject rootObject = d.object();
        QJsonValue pngNameJsonValue = rootObject.value(QString("result"));
    //    qWarning() << pngNameJsonValue.toString();

        QJsonValue framesJsonValue = rootObject.value(QString("resultData"));
    //    qWarning() << framesJsonValue;

        if(framesJsonValue.isArray()){

            QJsonArray array = framesJsonValue.toArray();
            for(int i = 0; i < array.size(); i++)
            {
                QJsonValue helloArrayValue = array.at(i);
                if(helloArrayValue.isObject())
                {
                     QJsonObject rootObject = helloArrayValue.toObject();
                     qDebug()<<rootObject.value(QString("menuImgName"));
                     qDebug()<<rootObject.value(QString("menuImgId")).toString();
                     qDebug()<<rootObject.value(QString("managerId")).toString();
                     qDebug()<<rootObject.value(QString("floor"));
                     qDebug()<<rootObject.value(QString("serialNum"));
                     qDebug()<<rootObject.value(QString("url"));
                     qDebug()<<"-------------------------";
                     setImageUrlToList(rootObject.value(QString("floor")).toString(),
                                       rootObject.value(QString("serialNum")).toDouble(),
                                       rootObject.value(QString("url")).toString());
                }
            }
        }
}
void getUrlImage::setImageUrlToList(QString floor,double serialNum,QString url)
{
    QString name;
    int isubindextemp = 0;
    name = "f" + floor + "p" + QString::number(serialNum);
    foreach(str_ImageUrl StrImageUrl,m_TagStrImageUrlList)
    {
        if(StrImageUrl.name == name)
        {
            isubindextemp = StrImageUrl.subindex;
        }
    }
    str_ImageUrl StrImageUrlTemp;
    StrImageUrlTemp.name = name;
    StrImageUrlTemp.subindex = isubindextemp + 1;
    StrImageUrlTemp.url = url;
    m_TagStrImageUrlList.append(StrImageUrlTemp);
}

void getUrlImage::startgetPictureList()
{
    m_getImageUrlIndex = 0;
    if(m_TagStrImageUrlList.count() > 0)
    {
        manager->get(QNetworkRequest(QUrl(m_TagStrImageUrlList.at(m_getImageUrlIndex).url)));
    }
}

void getUrlImage::replyFinished(QNetworkReply *reply)
{
    if(reply->error() == QNetworkReply::NoError)
    {
        //获取字节流构造 QPixmap 对象
        StrImage StrImageTemp;
        StrImageTemp.picture.loadFromData(reply->readAll());
        StrImageTemp.name = m_TagStrImageUrlList.at(m_getImageUrlIndex).name
                + "s" + QString::number(m_TagStrImageUrlList.at(m_getImageUrlIndex).subindex);
        TagStrImageList.append(StrImageTemp);
        m_getImageUrlIndex++;
        if(m_getImageUrlIndex < m_TagStrImageUrlList.count())
        {
            manager->get(QNetworkRequest(QUrl(m_TagStrImageUrlList.at(m_getImageUrlIndex).url)));
        }
    }
}
